public class ChangzuzheReversal {
    public static void main(String[] args) {
        Node head = null;
        Node pre = null;
        for (int i = 1;i <= 10;i++) {
            Node node = new Node();
            node.data = i+"";
            if (i == 1) {
                head = node;
            }
            if (pre != null) {
                pre.next = node;
            }
            pre = node;
        }

        System.out.println("before reversal:");
        printNode(head);
        //反转
        Node current = head;
        //记录最初的头部，之后永远设置原始头部指向下一个
        Node orginalHead = head;
        while(current.next != null) {
            //记录临时的头部
            Node tempHead = head;
            //将头部设置为下一个元素
            head = current.next;
            //将最初的头部指向像一个元素
            orginalHead.next = current.next.next;
            //将头部指向之前的临时头部
            head.next = tempHead;
        }
        System.out.println("after reversal:");
        printNode(head);
    }



    public static void printNode(Node node) {
        if (node != null) {
            System.out.print(node.data);
            if (node.next != null ) {
                System.out.print("->");
                printNode(node.next);
            } else {
                System.out.print("\n");
            }
        }
    }
}

class Node {
    String data;
    Node next;
}
